Imagine being tasked with exploring a vast underground cave system where no human has ever been before. Narrow passages, uneven ceilings, jagged rocks and sudden drops make it far too dangerous for people to enter. Instead, scientists use autonomous drones to safely explore these hidden environments, mapping caves and collecting valuable data without putting anyone at risk.
In this activity, you’ll investigate the Python code used to control a cave exploration drone. Rather than being controlled by a pilot, the drone uses three infra-red distance sensors to detect obstacles in front of it. One sensor points diagonally upwards, one points straight ahead, and one points diagonally downwards. As the drone flies through the cave, these sensors constantly monitor the shape of the ceiling and the ground.

The Python program processes the information from the three sensors many times every second. If one of the sensors detects that the drone is getting too close to the cave walls, the program decides whether the drone should climb, descend or continue flying straight ahead. By repeatedly reading the sensors and adjusting its movement, the drone can navigate safely through the cave while avoiding collisions.
Here is the flowchart for the algorithm to control the drone:

As you work through the code, you will discover how programming, sensors and decision-making algorithms combine to create an autonomous system capable of reacting to its environment in real time. See if you can understand how the drone makes its decisions, then experiment with the code to improve its navigation or add your own ideas!
Python Code
Your Turn
As part of a space exploration project, an autonomous drone spacecraft will be sent on a journey through Jupiter’s asteroid belt. Your task is to reuse the Python code developed for the cave-exploration drone and adapt it for this new mission. Instead of detecting the cave’s ceiling and ground, the drone must use its three laser sensors to identify approaching asteroids and automatically adjust its vertical position to avoid a collision. Can you modify the existing navigation algorithm so the spacecraft can travel safely through space?

Python Code
You will need to update the code from line 48.

Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area



